var bolLoad = true;

function jPage_setLoading(){
  var htmlLoading = '<img src="img/loading.gif" alt="Loading album list..." class="loading" height="100%" />';
	$("#divArtists").html(htmlLoading);
}

// Build page data with artists info
function jPage_build(res){

	$("#divArtists").html("");
	
	for (var i = 0; i < res.length; i++){
    var o = res[i];
    var divArtistHTML = jPage_createAlbumDiv(o);
    $("#divArtists").append(divArtistHTML);
  }
  
  if(bolLoad){
    bolLoad = false;
    if($.jPage_getUrlVar('album')){
      jPage_chooseAlbum($.jPage_getUrlVar('album'), res[0].artist_name);
    }else{
      jPage_chooseAlbum(res[0].album_id, res[0].artist_name);
    }
  }
}

function jPage_createAlbumDiv(o, i){
  return '<div id="divAlbum" onclick="jPage_chooseAlbum(' + o.album_id + ',\'' + o.artist_name + '\');" >' +
      '<img src="' + o.album_image + '" alt="" class="img" align="left" />' +
      '<span class="label">Album: </span><span class="value">' + o.album_name + '</span><br / >' +
      '<span class="label">Artist: </span><span class="value">' + o.artist_name + '</span><br / >' +
      '<span class="label">Genre: </span><span class="value">' + o.artist_genre + '</span><br / >' +
      '<span class="label">Location: </span><span class="value">' + o.location_city + '</span>' +
      '</div>';
}

function jPage_chooseAlbum(album_id, artist_name){
  jPage_setPlayer(album_id);
  
  jPage_setShare(album_id, artist_name);
  
	  // analytics
  _gaq.push(['_trackEvent', 'ChooseAlbum', loc.long_name, album_id]);
}

function jPage_setPlayer(album_id){
  var content =
	  '<object width="200" height="300"' +
	  ' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' +
	  ' codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"' +
	  ' align="middle">' +
	  ' ' +
	  ' <param name="allowScriptAccess" value="always" / >' +
	  ' <param name="wmode" value="transparent" / >' +
	  ' ' +
	  ' <param name="movie"' +
	  ' value="http://widgets.jamendo.com/en/album/?album_id=' +
	  album_id +
	  '&playerautoplay=1&playertype=2008&refuid=803300" / >' +
	  ' <param name="quality" value="high" / >' +
	  ' <param name="bgcolor" value="#FFFFFF" / >' +
	  ' <embed' +
	  ' src="http://widgets.jamendo.com/en/album/?album_id=' +
	  album_id +
	  '&playerautoplay=1&playertype=2008&refuid=803300"' +
	  ' quality="high" wmode="transparent" bgcolor="#FFFFFF" width="200" height="300" align="middle"' +
	  ' allowScriptAccess="always" type="application/x-shockwave-flash"' +
	  ' pluginspage="http://www.macromedia.com/go/getflashplayer">' +
	  ' </embed>' +
	  '</object>'
  ;

  $("#divPlayer").html(content);
}

function jPage_setShare(album_id, artist_name){

			var iframe_src_url =
				'http://www.facebook.com/plugins/like.php?'	+
				'href=' + 'http%3A%2F%2Fjammap.cassioso.com' +
				'%2F' +
				'index.html' + '%3F' +
				'album' + '%3D' + album_id  + '%26' +
				'loc'	+ '%3D' + loc.short_name +
				'&amp;' +
				'layout=standard'	+ '&amp;' +
				'show_faces=true'	+ '&amp;' +
				'width=200'		+ '&amp;' +
				'action=like'	+ '&amp;' +
				'font=trebuchet+ms'	+ '&amp;' + 
				'colorscheme=light'	+ '&amp;'
			;
			
			var facebook_like_button =
        '<iframe src="' + iframe_src_url + '" ' +
        'style="scrolling:no; frameborder:0px; border:none; overflow:hidden; width:200px; height:80px; display:inline; float: none; white-space: nowrap; text-decoration: none; allowTransparency:true">' +
				'</iframe>';
			
			var album_url = jMedia_getShortUrl(album_id);

      var tweet_button =
        '<a href="http://twitter.com/share" class="twitter-share-button" data-text="' + artist_name + ' on Jammap! ' +  
        album_url +
        '" data-count="none" data-via="CassioOliveira" data-related="CassioOliveira:Jamendo" style="{display:inline;float:none;white-space:nowrap;text-decoration:none;vertical-align:middle;margin:4px}">Tweet</a>' +
        '<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>'
      ;

			var content = '<br />' + tweet_button + '<br /><br />' + facebook_like_button + '<br /><br />' ;

			$("#divShareAlbum").html(content);
}

/*
	Gets a post var

	Ex.:
		<http://jammap.cassioso.com/index.html?album=48022&lat=21.207281176456437&lng=5.516536424999985>
		var album	  = $.jPage_getUrlVar('album');	// 48022
		var varLat	= $.jPage_getUrlVar('lat');	// 21.207281176456437
		var varLng	= $.jPage_getUrlVar('lng');	// 5.516536424999985

*/
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  jPage_getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
